using UnityEngine;
using QFramework;
using Unity.VisualScripting;
using NotImplementedException = System.NotImplementedException;

namespace ProjectSurvival
{
    public partial class EnemyMiniBoss : ViewController, IEnemy
    {
        [SerializeField]private bool _isTreasure = false;
        public enum States
        {
            FollowingPlayer,
            Warning, //进入警戒状态
            Dash, //冲向主角
            Wait,
        }

        //FSM状态机
        public FSM<States> fsm = new FSM<States>();

        public float moveSpeed = 2f;
        public float hp = 50;

        float duration = 0.5f;
        float timer = 0f;
        bool isWhite = true;
        public Color DissolveColor = Color.yellow;

        void Start()
        {
            EnemyGenerator.EnemyCount.Value++;
            //添加状态到状态机
            fsm.State(States.FollowingPlayer)
                .OnFixedUpdate(() =>
                {
                    if (Player.Instance)
                    {
                        var direction = (Player.Instance.transform.position - transform.position).normalized;
                        SelfRigidbody2D.velocity = direction * moveSpeed;
                        var playerDistance = Player.Instance.transform.position - transform.position;
                        if (playerDistance.magnitude < 15)
                        {
                            fsm.ChangeState(States.Warning);
                        }
                    }
                    else
                    {
                        SelfRigidbody2D.velocity = Vector2.zero;
                    }
                });

            fsm.State(States.Warning)
                .OnEnter(() =>
                {
                    SelfRigidbody2D.velocity = Vector2.zero;
                    duration = 0.5f;
                })
                .OnUpdate(() =>
                {
                    timer += Time.deltaTime;
                    if (timer >= duration)
                    {
                        if (isWhite)
                        {
                            Sprite.color = Color.red;
                            isWhite = false;
                        }
                        else
                        {
                            Sprite.color = Color.white;
                            isWhite = true;
                        }
                        if (duration > 0)
                        {
                            duration -= 0.05f;
                        }
                        timer = 0;
                    }

                    if (fsm.SecondsOfCurrentState >= 3f)
                    {
                        fsm.ChangeState(States.Dash);
                    }
                })
                .OnExit(() =>
                {
                    Sprite.color = Color.white;
                });
            var dashStartDistance = 0f;
            var dashCurrentDistance = 0f;
            fsm.State(States.Dash)
                .OnEnter(() =>
                {
                    var playerDistance = Player.Instance.transform.position - transform.position;
                    var direction = playerDistance.normalized;
                    dashStartDistance = playerDistance.magnitude;
                    SelfRigidbody2D.velocity = direction * 15f;
                })
                .OnUpdate(() =>
                {
                    dashCurrentDistance = (Player.Instance.transform.position - transform.position).magnitude;
                    if (dashCurrentDistance >= dashStartDistance * 1.6f)
                    {
                        fsm.ChangeState(States.Wait);
                    }
                });
            fsm.State(States.Wait)
                .OnEnter(() => { SelfRigidbody2D.velocity = Vector2.zero; })
                .OnUpdate(() =>
                {
                    if (fsm.SecondsOfCurrentState >= 3f)
                    {
                        fsm.ChangeState(States.FollowingPlayer);
                    }
                });
            fsm.StartState(States.FollowingPlayer);
        }

        private void OnDestroy()
        {
            EnemyGenerator.EnemyCount.Value--;
        }

        private void FixedUpdate()
        {
            fsm.FixedUpdate();
        }

        private void Update()
        {
            fsm.Update();
            if (hp <= 0)
            {
                Global.GeneratePowerUp(transform,_isTreasure);
                FxController.instance.Play(Sprite,DissolveColor);
                AudioKit.PlaySound("Enemy_die");
                gameObject.DestroySelfGracefully();
            }
        }

        private bool _ignoreHurt = false;

        public void Hurt(float hurtValue, bool force = false, bool critical = false)
        {
            if (_ignoreHurt && !force)
            {
                return;
            }

            //_ignoreHurt = true;
            Color origionalColor = Sprite.color;
            Sprite.color = Color.red;
            AudioKit.PlaySound("Hit");
            ActionKit.Delay(0.2f, () =>
            {
                hp -= hurtValue;
                FloatingTextController.PlayText(transform.position + Vector3.up * 0.5f, hurtValue.ToString("0"),critical);
                Sprite.color = origionalColor;
                _ignoreHurt = false;
            }).Start(this);
        }

        public void SetSpeedScale(float hpScaleValue)
        {
            this.hp *= hpScaleValue;
        }

        public void SetHpScale(float speedScaleValue)
        {
            this.moveSpeed *= speedScaleValue;
        }
    }
}